package com.taotao.content.service.impl;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.JsonUtils;
import com.taotao.content.service.ContentService;
import com.taotao.content.service.JedisClient;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
/**
 * 内容服务
 * @author Administrator
 *
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper mapper;
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${AD1_CATEGORYID}")
	private long AD1_CATEGORYID;
	
	@Override
	public TaotaoResult addContent(TbContent tbContent) {
		//1.注入Mapper
		//2.增加内容
		tbContent.setCreated(new Date());
		mapper.insert(tbContent);
		
		try {
			//保存的时候清空缓存
			jedisClient.hdel("CONTENT_KEY", 89 + "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		//3.返回TaotaoResult
		return TaotaoResult.ok(tbContent);
	}

	@Override
	public EasyUIDataGridResult showContent(Integer nowPage, Integer rows,long categoryId) {
		//1.设置分页信息
		if(null == nowPage) nowPage = 0;
		if(null == rows) rows = 20;
		PageHelper.startPage(nowPage, rows);
		//2.调用mapper
		/*TbContentExample example = new TbContentExample();
		example.createCriteria().andCategoryIdEqualTo(categoryId);
		List<TbContent> contents = mapper.selectByExample(example);*/
		List<TbContent> contents = mapper.getContentList(categoryId);

		//3.获得分页信息
		PageInfo<TbContent> info = new PageInfo<TbContent>(contents);
		//封装进EasyUIDataGridResult
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		
		result.setTotal((int) info.getTotal());
		result.setRows(info.getList());
		return result;
	}

	@Override
	public TaotaoResult deleteContent(List<Long> ids) {
		TbContentExample example = new TbContentExample();
		example.createCriteria().andIdIn(ids);
		mapper.deleteByExample(example);
		try {
			//删除的时候清空缓存
			jedisClient.hdel("CONTENT_KEY", 89 + "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return TaotaoResult.ok();
	}

	@Override
	public List<TbContent> getContentList() {
		//添加缓存不能影响正常的业务逻辑加trycatch
		
		try {
			//判断redis中是否有数据，如果有直接重redis中拿
			String str = jedisClient.hget("CONTENT_KEY", AD1_CATEGORYID + "");
			if (StringUtils.isNotEmpty(str)) {
				System.out.println("已有缓存");
				return JsonUtils.jsonToList(str, TbContent.class);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<TbContent> contents = mapper.getContentList(AD1_CATEGORYID);
		
		// 将数据写入redis
		//注入JedisClient
		//Map<自定义名称,Map<内容分类Id,所属分类Json串>>
		try {
			System.out.println("没缓存，加入缓存");
			jedisClient.hset("CONTENT_KEY", AD1_CATEGORYID + "", JsonUtils.objectToJson(contents));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return contents;
	}


}
